<?php

/**
 * This is the model class for table "{{user_file}}".
 *
 * The followings are the available columns in table '{{user_file}}':
 * @property integer $id
 * @property integer $file_id
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property User $user
 * @property File $file
 */
class UserAndFile extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return UserAndFile the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{user_file}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('id', 'required'),
	    array('id, file_id, user_id', 'numerical', 'integerOnly' => true),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, file_id, user_id', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
	    'file' => array(self::BELONGS_TO, 'FileModel', 'file_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'file_id' => 'File',
	    'user_id' => 'User',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('id', $this->id);
	$criteria->compare('file_id', $this->file_id);
	$criteria->compare('user_id', $this->user_id);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    /**
     * 
     * @param int $fileID
     * @param array $userIDs
     * @return array<UserAndFile>
     */
//	public static function searchUsersAssignedAFile($fileID, $userIDs)
//	{
//		// Warning: Please modify the following code to remove attributes that
//		// should not be searched.
//
//		$criteria=new CDbCriteria;
//
//		$criteria->compare('file_id', $fileID);
//		$criteria->addInCondition('user_id', $userIDs);
//		
//		return self::model()->findAll($criteria);
//	}

    public static function deleteAllRecordWithFileID($fileID) {
	if (isset($fileID) && intval($fileID) > 0) {
	    self::model()->deleteAllByAttributes(array('file_id' => $fileID));
	}
    }
    
    /**
     * 
     * @param type $fileID
     * @return \CActiveDataProvider
     */
    public static function getAllUserAssignedToAFile($fileID) {
	
	$criteria = new CDbCriteria;
	$criteria->alias = 't';
	$criteria->with = array('userFiles');
	$criteria->together = true;
	$criteria->compare('userFiles.file_id', $fileID);
	$criteria->order = 'username ASC';
	
	return new CActiveDataProvider(User::model(),array(
	    'criteria' => $criteria,
	    'pagination' => false,
	    'sort' => array(
		'defaultOrder' => array(
		    'username' => CSort::SORT_ASC
		)
		)));
	
	
	
    }

}
